#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
 @file name: draw_demo.py
 @desc:
 @author: xu wang
 @mail: jason_wangxu@163.com
 @date: 2024/9/21 16:34
"""
"""
    @file name：draw_graphs
    @desc ：
    @author：王旭
    @mail：wx370782@alibaba-inc.com
    @date：2024/8/5 14:17
"""
import threading
import time

import matplotlib.pyplot as plt
from queue import Queue
import matplotlib.ticker as ticker

q = Queue()


def read():
    with open(
            r"D:\git\YiTian\YiTian\reports\test_setting_app_240922-094515\cpu_top.csv",
            mode="r") as f:
        for index, line in enumerate(f):
            if index == 0:
                continue
            timestamp = line.split(",")[0].strip()
            value = float(line.split(",")[-1].strip())
            data = [timestamp, value]
            q.put(data)


t1 = threading.Thread(target=read).start()
time.sleep(1)

# 设置全局字体
plt.rcParams['font.sans-serif'] = 'SimHei'  # 设置默认字体为宋体
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
plt.title('折线图')
plt.xlabel('时间')
plt.ylabel('数值')
plt.xticks(rotation=90)
plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(base=10))

# 初始化数据列表
stamps = []
values = []

while not q.empty():
    # 假设我们从外部源获取两个新的值
    new_value_1 = q.get()[0]
    new_value_2 = q.get()[1]
    # 将新值添加到数据列表中
    stamps.append(new_value_1)
    values.append(new_value_2)
    # 更新图表
    x = [i for i in stamps]
    plt.plot(x, values, color='red')
    plt.pause(0.2)
